const App = getApp()

Page({
  data: {
    hidden: !0,
    cars: [],
    cars2: [],
    userId: '',
    type: 0,

  },
  onLoad (parm) {
    console.log(parm)
    let userId = wx.getStorageSync('userId');
    if (typeof(parm) != "undefined"){
      if (typeof (parm.type) != "undefined") {
        console.log(parm.type)
        let type = parm.type;
        this.setData({ type: type })
      }
    }
    
    this.setData({ userId: userId })
    wx.showNavigationBarLoading()
    this.getList(() => {
      wx.hideNavigationBarLoading()
    })
  },
  onPullDownRefresh() {
    let userId = wx.getStorageSync('userId');
    this.setData({ userId: userId })
    wx.showNavigationBarLoading()
    this.getList(() => {
      wx.hideNavigationBarLoading()
    })
  },
  radioChange (e) { // 设置默认车辆
    wx.hideLoading();
    let defaultCar = wx.getStorageSync('defaultCar');
    App.HttpService.carDefault({id: e.detail.value}).then(res => {
      defaultCar.carId = e.detail.value;
      App.HttpService.getDefault({userId:this.data.userId}).then(res => {
        if (res.data.code == 200) {
          let carInfo = res.data.data.carInfo;
          let carModel = res.data.data.carModel;
          let carInfos = carInfo.brandName + ' ' + carInfo.factoryName + ' ' + carInfo.modelName;
          let carModelId = carInfo.modelId;
          let carFactoryId = carInfo.factoryId;
          let miles = carModel.miles;
          let linkman = carModel.linkman;
          let phone = carModel.phone;
          let carCode = carModel.carAreaCode + carModel.carAreaNo;
          let brandId = carModel.brandId;
          defaultCar.carInfo = carInfos;
          defaultCar.carModelId = carModelId;
          defaultCar.carFactoryId = carFactoryId;
          defaultCar.miles = miles;
          defaultCar.linkman = linkman;
          defaultCar.phone = phone;
          defaultCar.carCode = carCode;
          defaultCar.brandId = brandId;
          wx.setStorageSync('defaultCar', defaultCar);
          if (this.data.type != 0 && this.data.type == 1) {
            //返回保养方案页面
            wx.redirectTo({
              url: `/pages/product/package/index?type=1&carInfo=${carInfos}&carModelId=${carModelId}&carFactoryId=${carFactoryId}&miles=${miles}&linkman=${linkman}&phone=${phone}&carCode=${carCode}&brandId=${brandId}`
            })

          }
        }
      });
      
      this.getList();
    })
  },
  toEdit (e) { // 编辑车辆
    let { id, brandName, factoryName, modelName } = e.currentTarget.dataset
    let carInfo = brandName + ' ' + factoryName + ' ' + modelName



    wx.navigateTo({
      url: `/pages/car/edit/index?id=${id}&carInfo=${carInfo}`
    })
  },
  toDel (e) { // 删除车辆
    let { id, isDefault } = e.currentTarget.dataset
    if (isDefault != 1) {
      wx.showModal({
        title: '删除',
        content: '确定是否删除此车辆',
        success: (res) => {
          if (res.confirm) {
            this.carDel(id)
          }
        }
      })
    } else {
      wx.showToast({
        title: '禁止删除默认车辆',
        image: '../../../assets/images/warning.png',
        duration: 1500
      })
    }
  },
  carDel (id) { // 删除车辆
    App.HttpService.carDel({id: id}).then(res => {
      this.getList()
    })
  },
  toAddCar () { // 跳转添加车辆
      // wx.navigateTo({
      //       url: '/pages/car/firstMenu/index'
      //     })
  // return


    wx.showActionSheet({
      itemList: ['自动添加', '手动添加'],
      success(res) {
        console.log(res.tapIndex)

        if(res.tapIndex==0){
          wx.navigateTo({
            url: '/pages/car/add/index?id='+1
          })

        }else{
          wx.navigateTo({
            url: '/pages/car/firstMenu/index'
          })

        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })


  },
  getList (cb) { // 获取车辆列表
    let { userId } = this.data;
    let cars = [];
    let cars2 = [];
    App.HttpService.userCars({userId: userId}).then(res => {
      cb && cb();
      let data = res.data;
      if (data.code == 200) {
        if (data.data.unavailable.length > 0) {
          cars2 = data.data.unavailable;
          this.setData({cars2: cars2});
        }
        cars = data.data.available;
        cars.forEach((n, i) => n.checked = n.isDefault === 1);
        this.setData({ cars: cars});
      } else {
        wx.showToast({
					title: data.msg,
					image: '../../../assets/images/warning.png',
					duration: 1500
				})
      }
    })
  },
  // 传送fromid
  fid: function (e) {
    // fromid
    let uid = wx.getStorageSync('userId')
    App.HttpService.fid({
      buyerId: uid,
      fromId: e
    }).then(res => {
      console.log(res)
    })

  },

  addfrom(e) {
    console.log('form', e)
    var that = this
    var formid = e.detail.formId
    let val = e.detail.value
    console.log(formid)

    this.fid(formid);

  },
})